﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace LightCAD.Core.Elements
{
    /// <summary>
    /// 直接组件元素，没有实例化，例如墙、管道、风管、线缆、桥架等等
    /// </summary>
    public class DirectComponent : LcElement, IComponenetElement
    {
        /// <summary>
        /// 基准线，例如墙体拖拽编辑的线
        /// </summary>
        public ICurve2d BaseCurve { get; set; }
        /// <summary>
        /// 外形线，例如墙的外形
        /// </summary>
        public List<ICurve2d> Curves { get; internal set; } = new List<ICurve2d>();
        public List<Vector2d> Points { get; set; } = new List<Vector2d>();

        public DirectComponent()
        {
            this.Type = BuiltinElementType.DirectComponent;
        }

        public override ICurve2d[] GetCurves()
        {
            var curves = new List<ICurve2d>();

            curves.AddRange(Curves);

            return curves.ToArray();
        }
        public virtual void ResetShapeCurves()
        {

        }
        public override void Copy(LcElement src)
        {
            base.Copy(src);
            var dirCom = src as DirectComponent;
            this.BaseCurve = dirCom.BaseCurve.Clone();
            this.Curves = dirCom.Curves.Clone();
            this.Points = dirCom.Points.Clone();

        }
        public override LcElement Clone()
        {
            throw new NotImplementedException();
        }

        public List<AssociatePoint> AssociatePoints { get; set; } = new List<AssociatePoint>();

        public List<AssociateElement> AssociateElements { get; set; } = new List<AssociateElement>();

        public string Material { get; set; }
        public string MaterialSettings { get; set; }

        public virtual void Invoke(string methodName, InvokeToken token)
        {
        }
        public override Box2d GetBoundingBox()
        {
            var line2d = (BaseCurve as Line2d);
            return new Box2d(line2d.Start, line2d.End);
        }
        public override Box2d GetBoundingBox(Matrix3d matrix)
        {
            var line2d = (BaseCurve as Line2d);
            return new Box2d(matrix.MultiplyPoint(line2d.Start), matrix.MultiplyPoint(line2d.End));
        }

        public override void Translate(double dx, double dy)
        {
            //var line2d = this.BaseCurve as Line2d;
            //var ts = new Vector2d(line2d.Start.X + dx, line2d.Start.Y + dy);
            //var te = new Vector2d(line2d.End.X + dx, line2d.End.Y + dy);
            //Set(ts, te);
            //ResetBoundingBox();
            //ResetShapeCurves();

        }


        public override void WriteProperties(Utf8JsonWriter writer, JsonSerializerOptions soptions)
        {
            base.WriteProperties(writer, soptions);

            writer.WriteObjectProperty(nameof(BaseCurve), this.BaseCurve, soptions);
            writer.WriteCollectionProperty<ICurve2d>(nameof(Curves), this.Curves, soptions,(w,c)=>w.WriteCurve2dObject(c,soptions));
            writer.WriteCollectionProperty<Vector2d>(nameof(Points), this.Points, soptions);
        }
        public override void ReadProperties(ref JsonElement jele)
        {
            base.ReadProperties(ref jele);

           this.BaseCurve = jele.ReadCurve2dProperty(nameof(BaseCurve));
           this.Curves=  jele.ReadCurve2dListProperty(nameof(Curves));
            this.Points=  (List<Vector2d>)jele.ReadListProperty<Vector2d>(nameof(Points));
        }
    }
}
